﻿//********************************************************************************//
//创建日期：<创建日期，2017-03-16>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：地表水开账
//********************************************************************************//

using System;
using System.Collections.Generic;
using System.Data;
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS.ShouFeiGL
{
    public partial class KaiZhang_DBS : BasePage
    {
        #region 属性

        /// <summary>
        ///     保存结果集1
        /// </summary>
        private DataTable dtKZXX
        {
            get { return ViewState["dtKZXX"] as DataTable; }
            set { ViewState["dtKZXX"] = value; }
        }

        #endregion

        protected void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            var cbHead = (ASPxCheckBox) sender;
            var count = gvYHXX.VisibleRowCount;
            if (cbHead.Checked)
                for (var i = 0; i < count; i++)
                {
                    var cks = gvYHXX.FindRowCellTemplateControl(i, null, "Ch") as ASPxCheckBox;
                    cks.Checked = true;
                }
            else
                for (var i = 0; i < count; i++)
                {
                    var cks = gvYHXX.FindRowCellTemplateControl(i, null, "Ch") as ASPxCheckBox;
                    cks.Checked = false;
                }
        }

        #region 变量

        /// <summary>
        ///     用户信息bll
        /// </summary>
        private QS_YG_YONGHUXXBLL _YHXXBLL = new QS_YG_YONGHUXXBLL();

        /// <summary>
        ///     存储过程调用类
        /// </summary>
        private readonly SpHelper _SpHelper = new SpHelper();

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack) InitData();
            //this.ScriptManager1.RegisterPostBackControl(this.btnExport);
            LoadGridData();
        }


        /// <summary>
        ///     开账
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnKZ_Click(object sender, EventArgs e)
        {
            if (dtKZXX != null)
            {
                var sYhh = "";
                //获取选中的数据
                var dt = PageTools.GetCheckedData(gvYHXX, dtKZXX, "cid", "id");
                if (dt.Rows.Count > 0)
                {
                    var yhhs = new List<String>();
                    for (var i = 0; i < dt.Rows.Count; i++)
                        if (Convert.ToInt32(dt.Rows[i]["KZSL"]) > 0)
                        {
                            yhhs.Add(dt.Rows[i]["S_YONGHUH"].ToString());
                        }
                    sYhh = "'";
                    //筛选及重命名需要的列
                    for (var i = 0; i < yhhs.Count; i++)
                        if (i == yhhs.Count - 1)
                            sYhh += yhhs[i] + "'";
                        else
                            sYhh += yhhs[i] + "','";
                }

                var yd = txtShuiLiangYM.Text;
                var zwym = BusinessTool.GetSLYMorZWNY(2, 1);
                //int month = DateTime.Now.Month;
                //if (zwym != month-1)
                //{
                //    zwym = month - 1;
                //}


                //开账
                var dt2 = _SpHelper.P_QSSF_KAIZHANG_DBS(sYhh, "0", Convert.ToInt32(yd.Substring(0, 4)),
                    Convert.ToInt32(yd.Substring(4, 2)), LoginInfo.CurrentUser.UserID.ToString(), zwym).Tables[0];
                if (dt2.Rows[0][0].ToString() == "1")
                {
                    //所选择的用户开张成功
                    PageTools.AlertMsg(this, "所选择的用户开账成功");
                    LoadGridData();
                }
            }
        }


        protected void gvYHXX_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadGridData();
        }

        #endregion

        #region 方法

        /// <summary>
        ///     初始化数据
        /// </summary>
        private void InitData()
        {
            // gvYHXX.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            //水量年月
            var i_ym = BusinessTool.GetSLYMorZWNY(1, 1);
            txtShuiLiangYM.Text = i_ym.ToString();
            LoadGridData();
        }


        /// <summary>
        ///     加载查询数据
        /// </summary>
        private void LoadGridData()
        {
            var WTYEP = Convert.ToInt32(Request.QueryString["wtype"]);
            var ym = Convert.ToInt32(txtShuiLiangYM.Text);
            dtKZXX = _SpHelper.P_QSDBS_KZLB().Tables[0];
            if (dtKZXX != null && dtKZXX.Rows.Count > 0) zjl.Text = "共" + dtKZXX.Rows.Count + "条";

            var list = PageTools.GetCheckedValueList(gvYHXX, "cid", "id");
            gvYHXX.DataSource = dtKZXX;
            gvYHXX.DataBind();
            decimal zje = 0;
            if (dtKZXX.Rows.Count > 0)
            {
                for (var i = 0; i < dtKZXX.Rows.Count; i++) zje += Convert.ToDecimal(dtKZXX.Rows[i]["KZSLJE"]);
                txtKZJE.Text = zje.ToString();
            }

            //绑定后，给每一行的checkbox的clientname赋值
            for (var i = 0; i < gvYHXX.VisibleRowCount; i++)
            {
                var cb = (ASPxCheckBox) gvYHXX.FindRowCellTemplateControl(i, (GridViewDataColumn) gvYHXX.Columns[0],
                    "cid");
                cb.ClientInstanceName = "checked" + i;
            }

            //绑定后默认全选
            PageTools.SetCheckedAll(gvYHXX, "cid", true);
            //重新选中项
            PageTools.SetCheckedValueList(gvYHXX, "cid", "id", list);
        }

        #endregion
    }
}